package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.uis.beans.GiftBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Martin on 2017/1/11.
 */

public class GiftListAdapter extends BaseAdapter {

    private List<GiftBean> data;
    private LayoutInflater inflater;
    private Context mContext;

    int selectNum = 0 ;

    public GiftListAdapter(List<GiftBean> data, Context context) {
        if (data == null)
            data = new ArrayList<>();
        this.data = data;
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
    }

    public List<GiftBean> getData() {
        return data;
    }

    public void refresh( int pos){
        selectNum = pos ;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.dialog_item_gift,parent,false);
            holder = new ViewHolder(convertView);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        GiftBean giftBean = data.get(position);
        if (null != giftBean){
//            if (giftBean.isSelecte()){
            if (position == selectNum){
                holder.lineTop.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
                holder.lineBottom.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
                holder.lineLeft.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
                holder.lineRight.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
                holder.lineTop.setVisibility(View.VISIBLE);
                holder.lineBottom.setVisibility(View.VISIBLE);
                holder.lineLeft.setVisibility(View.VISIBLE);
                holder.lineRight.setVisibility(View.VISIBLE);
            }else {
                holder.lineTop.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                holder.lineBottom.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                holder.lineLeft.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                holder.lineRight.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                holder.lineTop.setVisibility(View.GONE);
                holder.lineBottom.setVisibility(View.VISIBLE);
                holder.lineLeft.setVisibility(View.GONE);
                holder.lineRight.setVisibility(View.GONE);
                itemLineManager(holder, position);
            }

            if (0 != giftBean.getImage()){
                holder.headImg.setBackgroundResource(giftBean.getImage());
            }else {

            }

            holder.nickName.setText(giftBean.getName());

        }

        convertView.setTag(holder);
        return convertView;
    }

    private void itemLineManager(ViewHolder holder, int position){
//        if (position > 4 ){
//            holder.lineTop.setVisibility(View.VISIBLE);
//        }
        if (position % 4 != 0){
            holder.lineLeft.setVisibility(View.VISIBLE);
        }
//        if (position % 4 != 3){
//            holder.lineRight.setVisibility(View.VISIBLE);
//        }
    }

    private class ViewHolder {

        ImageView headImg;
        TextView nickName;

        View lineTop, lineBottom, lineLeft, lineRight ;

        public ViewHolder(View itemView) {
            headImg = (ImageView) itemView.findViewById(R.id.image_gift);
            nickName = (TextView) itemView.findViewById(R.id.txt_name);

            lineTop = itemView.findViewById(R.id.line_top);
            lineBottom = itemView.findViewById(R.id.line_bottom);
            lineLeft = itemView.findViewById(R.id.line_left);
            lineRight = itemView.findViewById(R.id.line_right);

        }
    }
}
